package com.jj.fly.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.jj.fly.po.News;
import com.jj.fly.po.NewsMessage;
import com.jj.fly.po.TextMessage;
import com.thoughtworks.xstream.XStream;

/**
 * 消息格式转换工具类
 * 
 * @author JJ
 *
 */
public class MessageUtils {

	public static String MESSAGE_TEXT = "text";
	public static String MESSAGE_NEWS = "news";
	public static String MESSAGE_IMAGE = "image";
	public static String MESSAGE_VOICE = "voice";
	public static String MESSAGE_LINK = "link";
	public static String MESSAGE_LOCATION = "location";
	public static String MESSAGE_EVENT = "event";
	public static String MESSAGE_SUBSCRIBE = "subscribe";
	public static String MESSAGE_UNSUBSCRIBE = "unsubscribe";
	public static String MESSAGE_CLICK = "CLICK";
	public static String MESSAGE_VIEW = "VIEW";

	/**
	 * 将xml格式转换为Map
	 * 
	 * @param request
	 * @return
	 * @throws IOException
	 * @throws DocumentException
	 */
	public static Map<String, String> xmlToMap(HttpServletRequest request)
			throws IOException, DocumentException {
		Map<String, String> map = new HashMap<String, String>();
		SAXReader reader = new SAXReader();
		// 从request中获取输入流
		InputStream in = request.getInputStream();
		Document document = reader.read(in);
		// 获取根元素
		Element root = document.getRootElement();
		List<Element> list = root.elements();
		for (Element element : list) {
			map.put(element.getName(), element.getText());
		}
		in.close();
		return map;
	}

	/**
	 * 将文本消息转换为xml
	 * 
	 * @param textMessage
	 * @return
	 */
	public static String textMessageToXml(TextMessage textMessage) {
		XStream xStream = new XStream();
		xStream.alias("xml", textMessage.getClass());
		return xStream.toXML(textMessage);
	}

	public static String initText(String toUserName, String fromUserName,
			String content) {
		TextMessage textMessage = new TextMessage();
		textMessage.setFromUserName(toUserName);
		textMessage.setToUserName(fromUserName);
		textMessage.setMsgType(MESSAGE_TEXT);
		textMessage.setCreateTime(new Date().getTime());
		textMessage.setContent(content);
		return MessageUtils.textMessageToXml(textMessage);
	}

	/**
	 * 主菜单
	 * 
	 * @return
	 */
	public static String menuText() {
		StringBuffer sb = new StringBuffer();
		sb.append("欢迎您的关注，请按照菜单提示进行操作：\n\n")//
				.append("1：关注俊俊&晶晶：\n")//
				.append("2：相约金殿：\n")//
				.append("?：调出此菜单。");
		return sb.toString();
	}

	public static String firstMenu() {
		StringBuffer sb = new StringBuffer();
		sb.append("欢迎关注注俊俊&晶晶的公众号~");
		return sb.toString();
	}

	public static String secondMenu() {
		StringBuffer sb = new StringBuffer();
		sb.append("this is first menu");
		return sb.toString();
	}

	/**
	 * 图文消息转换为xml格式
	 * 
	 * @param newsMessage
	 * @return
	 */
	public static String newsMessageToXml(NewsMessage newsMessage) {
		XStream xStream = new XStream();
		xStream.alias("xml", newsMessage.getClass());
		xStream.alias("item", new News().getClass());
		return xStream.toXML(newsMessage);
	}

	/**
	 * 组装图文消息
	 * 
	 * @param toUserName
	 * @param fromUserName
	 * @return
	 */
	public static String initNewsMessage(String toUserName, String fromUserName) {
		String message = null;
		List<News> newsList = new ArrayList<News>();
		NewsMessage newsMessage = new NewsMessage();

		News news = new News();
		news.setTitle("相约金殿");
		news.setDescription("在经历了略显羞涩的早餐过后，我们聊得渐入佳境，不知不觉已经到了金殿脚下。小哥抬头一看，还好眼前这座小山不是很高，心里默默地呵呵了几下之后，就对这座无比高大的小山开启了散步模式。沿途看到来来往往的人群，上到六七十岁的老头在占领了一块开阔地之后，无情地拉起了他手中那么那么销魂的二胡，下到熟睡在婴儿车里祖国未来的花朵。不禁让我觉得这就是幸福，和心爱の人一起漫步于清晨の阳光之下，倾听她の关于，牵着她の手一起慢慢变老！");
		news.setPicUrl("http://aex.91geek.net/weixin/images/1.jpg");
		news.setUrl("www.baidu.com");

		News christmas = new News();
		christmas.setTitle("情定圣诞");
		christmas
				.setDescription("亲爱的筱筱：\n\n请原谅我这封迟到的告白，不是我对你不认真，因为圣诞是一年里最后也是最适合告白的日子。梦幻的街灯，彩色的铜铃，欢乐的音乐与甜蜜的气氛，我想在这个特别的日子里对特别的你说一句：我爱你！这封迟到的情书，献给最爱的你。");
		christmas.setPicUrl("http://aex.91geek.net/weixin/images/2.jpg");
		christmas.setUrl("www.baidu.com");

		newsList.add(news);
		newsList.add(christmas);
		newsMessage.setToUserName(fromUserName);
		newsMessage.setFromUserName(toUserName);
		newsMessage.setCreateTime(new Date().getTime());
		newsMessage.setArticles(newsList);
		newsMessage.setArticleCount(newsList.size());
		newsMessage.setMsgType(MESSAGE_NEWS);

		message = newsMessageToXml(newsMessage);
		return message;
	}
}
